import {createRouter, createWebHistory} from "vue-router";
import {useInfoStore} from "@/store/user/user.ts";


const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/test',
            name: 'test',
            component: () => import('@/views/test/test.vue')
        },
        {
            path: '/home',
            name: 'Home',
            component: () => import('@/components/jobseeker/layout/index.vue')
        },
        {
            path: '/login',
            name: 'Login',
            component: () => import('../views/Login.vue')
        },
        {
            path: '/register',
            name: 'Register',
            component: () => import('../views/Register.vue')
        },
        {
            path: '/admin',
            name: 'Admin',
            component: () => import('@/components/admin/layout/index.vue'),
            children: [
                {
                    path: 'home',
                    name: 'AdminHome',
                    component: () => import('@/views/admin/home/index.vue'),
                    meta: {
                        title: '数据展示大屏',
                        requiresAuth: true,
                        pathName: 'home',
                        titleName: 'home',
                    }
                },
                {
                    path: 'personal',
                    name: 'adminPersonal', //个人中心
                    component: () => import('@/views/admin/personal/index.vue'),
                    meta: {
                        title: '个人中心',
                        requiresAuth: true,
                        pathName: 'personal',
                        titleName: 'personal',
                    }
                },
                {
                    path: 'manageCheck',
                    name: 'adminManageCheck',
                    component: () => import('@/views/admin/manageCheck/index.vue'),
                    meta: {
                        title: '审核管控',
                        requiresAuth: true,
                        pathName: 'manageCheck',
                        titleName: 'manageCheck/checkList',
                    },
                    children: [
                        {
                            path: 'checkList',
                            name: 'adminManageCheckCheckList',
                            component: () => import('@/views/admin/manageCheck/checkList/index.vue'),
                            meta: {
                                title: '审核列表',
                                requiresAuth: true,
                                pathName: 'checkList',
                                titleName: 'manageCheck/checkList',
                            }
                        },
                        {
                            path: 'verificationAndEmployerDetail/:verificationId',
                            name: 'verificationAndEmployerDetail',
                            component: () => import('@/views/admin/manageCheck/checkDetail/employer.vue'),
                            meta: {
                                title: '企业审核详情',
                                requiresAuth: true,
                                pathName: 'verificationAndEmployerDetail',
                                titleName: 'manageCheck/checkList',
                            }
                        },
                        {
                            path: 'verificationAndJobseekerDetail/:verificationId',
                            name: 'verificationAndJobseekerDetail',
                            component: () => import('@/views/admin/manageCheck/checkDetail/jobseeker.vue'),
                            meta: {
                                title: '学生审核详情',
                                requiresAuth: true,
                                pathName: 'verificationAndJobseekerDetail',
                                titleName: 'manageCheck/checkList',
                            }
                        },
                        {
                            path: 'verificationAndResumeDetail/:verificationId',
                            name: 'verificationAndResumeDetail',
                            component: () => import('@/views/admin/manageCheck/checkDetail/resume.vue'),
                            meta: {
                                title: '荣誉审核详情',
                                requiresAuth: true,
                                pathName: 'verificationAndResumeDetail',
                                titleName: 'manageCheck/checkList',
                            }
                        },
                    ]
                },
                {
                    path: 'manageBulletin',
                    name: 'adminManageBulletin',
                    component: () => import('@/views/admin/manageBulletin/index.vue'),
                    meta: {
                        title: '公告管理',
                        requiresAuth: true,
                        pathName: 'manageBulletin',
                        titleName: 'manageBulletin/bulletinList',
                    },
                    children: [
                        {
                            path: 'addBulletin',
                            name: 'addBulletin',
                            component: () => import('@/views/admin/manageBulletin/addBulletin/index.vue'),
                            meta: {
                                title: '添加公告',
                                requiresAuth: true,
                                pathName: 'addBulletin',
                                titleName: 'manageBulletin/bulletinList',
                            }
                        },
                        {
                            path: 'bulletinList',
                            name: 'AdminManageBulletinList',
                            component: () => import('@/views/admin/manageBulletin/bulletinList/index.vue'),
                            meta: {
                                title: '公告列表',
                                requiresAuth: true,
                                pathName: 'bulletinList',
                                titleName: 'manageBulletin/bulletinList',
                            }
                        },
                        {
                            path: 'bulletinDetail/:bulletinId',
                            name: 'bulletinDetail',
                            component: () => import('@/views/admin/manageBulletin/bulletinDetail/index.vue'),
                            meta: {
                                title: '公告详情',
                                requiresAuth: true,
                                pathName: 'bulletinDetail',
                                titleName: 'manageBulletin/bulletinList',
                            }
                        },
                    ]
                },
                {
                    path: 'manageJob',
                    name: 'ManageJob',
                    component: () => import('@/views/admin/manageJob/index.vue'),
                    children: [
                        {
                            path: 'jobList',
                            name: 'ManageJobJobList',
                            component: () => import('@/views/admin/manageJob/jobList/index.vue'),
                            meta: {
                                title: '岗位列表',
                                requiresAuth: true,
                                pathName: 'jobList',
                                titleName: 'manageJob/jobList',
                            }
                        },
                        {
                            path: 'adminJobDetail/:jobId',
                            name: 'ManageJobJobDetail',
                            component: () => import('@/views/admin/manageJob/jobDetail/adminJobDetail.vue'),
                            meta: {
                                title: '岗位详情',
                                requiresAuth: true,
                                pathName: 'ManageJobJobDetail',
                                titleName: 'manageJob/jobList',
                            }
                        }
                    ]
                },
                {
                    path: 'manageEmployer',
                    name: 'ManageEmployer',
                    component: () => import('@/views/admin/manageEmployer/index.vue'),
                    children: [
                        {
                            path: 'employerList',
                            name: 'ManageEmployerEmployerList',
                            component: () => import('@/views/admin/manageEmployer/employerList/index.vue'),
                            meta: {
                                title: '企业列表',
                                requiresAuth: true,
                                pathName: 'employerList',
                                titleName: 'manageEmployer/employerList',
                            }
                        },
                        {
                            path: 'adminEmployerDetail/:employerId',
                            name: 'ManageEmployerEmployerDetail',
                            component: () => import('@/views/admin/manageEmployer/employerDetail/adminEmployerDetail.vue'),
                            meta: {
                                title: '企业详情',
                                requiresAuth: true,
                                pathName: 'ManageEmployerEmployerDetail',
                                titleName: 'manageEmployer/employerList',
                            }
                        }
                    ]
                },
                {
                    path: 'manageJobseeker',
                    name: 'ManageJobseeker',
                    component: () => import('@/views/admin/manageJobseeker/index.vue'),
                    children: [
                        {
                            path: 'jobseekerList',
                            name: 'ManageJobseekerJobseekerList',
                            component: () => import('@/views/admin/manageJobseeker/jobseekerList/index.vue'),
                            meta: {
                                title: '求职者列表',
                                requiresAuth: true,
                                pathName: 'jobseekerList',
                                titleName: 'manageJobseeker/jobseekerList',
                            }
                        },
                        {
                            path: 'adminJobseekerDetail/:jobseekerId',
                            name: 'ManageJobseekerJobseekerDetail',
                            component: () => import('@/views/admin/manageJobseeker/jobseekerDetail/adminJobseekerDetail.vue'),
                            meta: {
                                title: '求职者详情',
                                requiresAuth: true,
                                pathName: 'ManageJobseekerJobseekerDetail',
                                titleName: 'manageJobseeker/jobseekerList',
                            }
                        }
                    ]
                },
                {
                    path: 'manageReport',
                    name: 'ManageReport',
                    component: () => import('@/views/admin/manageReport/index.vue'),
                    children: [
                        {
                            path: 'reportList',
                            name: 'ManageReportReportList',
                            component: () => import('@/views/admin/manageReport/reportList/index.vue'),
                            meta: {
                                title: '求职者列表',
                                requiresAuth: true,
                                pathName: 'reportList',
                                titleName: 'manageReport/reportList',
                            }
                        },
                        {
                            path: 'adminReportDetail/:reportId',
                            name: 'ManageReportReportDetail',
                            component: () => import('@/views/admin/manageReport/reportDetail/adminReportDetail.vue'),
                            meta: {
                                title: '求职者详情',
                                requiresAuth: true,
                                pathName: 'ManageReportReportDetail',
                                titleName: 'manageReport/reportList',
                            }
                        }
                    ]
                }
            ]
        },
        {
            path: '/jobseeker',
            name: 'jobseeker',
            component: () => import('@/components/jobseeker/layout/index.vue'),
            children: [
                {
                    path: 'home',
                    name: 'jobseekerHome',
                    component: () => import('@/views/jobseeker/home/index.vue'),
                    meta: {
                        title: '首页',
                        requiresAuth: true,
                        pathName: 'home',
                        titleName: 'home',
                    }
                },
                {
                    path: 'personal',
                    name: 'jobseekerPersonal', //个人中心
                    component: () => import('@/views/jobseeker/personal/index.vue'),
                    meta: {
                        title: '个人中心',
                        requiresAuth: true,
                        pathName: 'personal',
                        titleName: 'personal',
                    }
                },
                {
                    path: 'suggest',
                    name: 'jobseekerSuggest',
                    component: () => import('@/views/jobseeker/suggest/index.vue'),
                    meta: {
                        title: '推荐',
                        requiresAuth: true,
                        pathName: 'suggest',
                        titleName: 'suggest/jobList',
                    },
                    children: [
                        {
                            path: 'jobList',
                            name: 'jobseekerSuggestJobList',
                            component: () => import('@/views/jobseeker/suggest/jobList/index.vue'),
                            meta: {
                                title: '岗位列表',
                                requiresAuth: true,
                                pathName: 'jobList',
                                titleName: 'suggest/jobList',
                            }
                        },
                        {
                            path: 'jobAndEmployerDetail/:jobId',
                            name: 'jobAndEmployerDetail',
                            component: () => import('@/views/jobseeker/suggest/jobDetail/JobAndEmployerDetail.vue'),
                            meta: {
                                title: '岗位详情',
                                requiresAuth: true,
                                pathName: 'jobDetail',
                                titleName: 'suggest/jobList',
                            }
                        }
                    ]
                },
                {
                    path: 'filter',
                    name: 'jobseekerFilter',
                    component: () => import('@/views/jobseeker/filter/index.vue'),
                    meta: {
                        title: '筛选',
                        requiresAuth: true,
                        pathName: 'filter',
                        titleName: 'filter',
                    }
                },
                {
                    path: 'collect',
                    name: 'jobseekerCollect',
                    component: () => import('@/views/jobseeker/collect/index.vue'),
                    meta: {
                        title: '收藏',
                        requiresAuth: true,
                        pathName: 'collect',
                        titleName: 'collect/collectList',
                    },
                    children: [
                        {
                            path: 'collectList',
                            name: 'collectList',
                            component: () => import('@/views/jobseeker/collect/collectList/index.vue'),
                            meta: {
                                title: '收藏列表',
                                requiresAuth: true,
                                pathName: 'collectList',
                                titleName: 'collect/collectList',
                            }
                        },
                        {
                            path: 'employerDetail/:employerId',
                            name: 'employerDetail',
                            component: () => import('@/views/jobseeker/collect/employerDetail/EmployerDetail.vue'),
                            meta: {
                                title: '公司详情',
                                requiresAuth: true,
                                pathName: 'employerDetail',
                                titleName: 'collect/collectList',
                            }
                        }
                    ]
                },
                {
                    path: 'new',
                    name: 'jobseekerNew',
                    component: () => import('@/views/jobseeker/new/index.vue'),
                    meta: {
                        title: '消息',
                        requiresAuth: true,
                        pathName: 'new',
                        titleName: 'new/interviewNewList',
                    },
                    children: [
                        {
                            path: 'interviewNewList',
                            name: 'interviewNewList',
                            component: () => import('@/views/jobseeker/new/interviewNewList/index.vue'),
                            meta: {
                                title: '面试消息列表',
                                requiresAuth: true,
                                pathName: 'interviewNewList',
                                titleName: 'new/interviewNewList',
                            }
                        },
                        {
                            path: 'interviewNewDetail/:interviewId',
                            name: 'interviewNewDetail',
                            component: () => import('@/views/jobseeker/new/interviewNewList/interviewNewDetail.vue'),
                            meta: {
                                title: '面试消息详情',
                                requiresAuth: true,
                                pathName: 'interviewNewDetail',
                                titleName: 'new/interviewNewList',
                            }
                        }
                    ]
                },
                {
                    path: 'offer',
                    name: 'jobseekerOffer',
                    component: () => import('@/views/jobseeker/offer/index.vue'),
                    meta: {
                        title: 'offer',
                        requiresAuth: true,
                        pathName: 'offer',
                        titleName: 'offer/offerList',
                    },
                    children: [
                        {
                            path: 'offerList',
                            name: 'offerList',
                            component: () => import('@/views/jobseeker/offer/offerList/index.vue'),
                            meta: {
                                title: 'offer列表',
                                requiresAuth: true,
                                pathName: 'offerList',
                                titleName: 'offer/offerList',
                            }
                        },
                        {
                            path: 'offerDetail/:offerId',
                            name: 'offerDetail',
                            component: () => import('@/views/jobseeker/offer/offerDetail/index.vue'),
                            meta: {
                                title: '岗位详情',
                                requiresAuth: true,
                                pathName: 'offerDetail',
                                titleName: 'offer/offerList',
                            }
                        },
                    ]
                },
                {
                    path: 'resume',
                    name: 'jobseekerResume',
                    component: () => import('@/views/jobseeker/resume/index.vue'),
                    meta: {
                        title: '简历',
                        requiresAuth: true,
                        pathName: 'resume',
                        titleName: 'resume',
                    }
                },
                {
                    path: 'report',
                    name: 'report',
                    component: () => import('@/views/jobseeker/report/index.vue'),
                    meta: {
                        title: '举报',
                        requiresAuth: true,
                        pathName: 'report',
                        titleName: 'report',
                    },
                },
                {
                    path: 'reportDetail/:reportId',
                    name: 'reportDetail',
                    component: () => import('@/views/jobseeker/report/reportDetail.vue'),
                    meta: {
                        title: '举报详情',
                        requiresAuth: true,
                        pathName: 'reportDetail',
                        titleName: 'report',
                    }
                },
            ]
        },
        {
            path: '/employer',
            name: 'employer',
            component: () => import('@/components/employer/layout/index.vue'),
            children: [
                {
                    path: 'home',
                    name: 'employerHome',
                    component: () => import('@/views/employer/home/index.vue'),
                    meta: {
                        title: '首页',
                        requiresAuth: true,
                        pathName: 'home',
                        titleName: 'home',
                    }
                },
                {
                    path: 'resumeDetail/:resumeId',
                    name: 'resumeDetail',
                    component: () => import('@/views/employer/home/resumeDetail.vue'),
                    meta: {
                        title: '岗位详情',
                        requiresAuth: true,
                        pathName: 'resumeDetail',
                        titleName: 'home',
                    }
                },
                {
                    path: 'personal',
                    name: 'employerPersonal', //个人中心
                    component: () => import('@/views/employer/personal/index.vue'),
                    meta: {
                        title: '个人中心',
                        requiresAuth: true,
                        pathName: 'personal',
                        titleName: 'personal',
                    }
                },
                {
                    path: 'manageJob',
                    name: 'employerManageJob',
                    component: () => import('@/views/employer/manageJob/index.vue'),
                    meta: {
                        title: '岗位管理',
                        requiresAuth: true,
                        pathName: 'manageJob',
                        titleName: 'manageJob/jobList',
                    },
                    children: [
                        {
                            path: 'addJob',
                            name: 'addJob',
                            component: () => import('@/views/employer/manageJob/addJob/index.vue'),
                            meta: {
                                title: '添加岗位',
                                requiresAuth: true,
                                pathName: 'addJob',
                                titleName: 'manageJob/jobList',
                            }
                        },
                        {
                            path: 'jobList',
                            name: 'EmployerManageJobJobList',
                            component: () => import('@/views/employer/manageJob/jobList/index.vue'),
                            meta: {
                                title: '岗位列表',
                                requiresAuth: true,
                                pathName: 'jobList',
                                titleName: 'manageJob/jobList',
                            }
                        },
                        {
                            path: 'jobDetail/:jobId',
                            name: 'jobDetail',
                            component: () => import('@/views/employer/manageJob/jobDetail/index.vue'),
                            meta: {
                                title: '岗位详情',
                                requiresAuth: true,
                                pathName: 'jobDetail',
                                titleName: 'manageJob/jobList',
                            }
                        },
                    ]
                },
                {
                    path: 'manageSend',
                    name: 'employerManageSend',
                    component: () => import('@/views/employer/manageSend/index.vue'),
                    meta: {
                        title: '投递管理',
                        requiresAuth: true,
                        pathName: 'manageSend',
                        titleName: 'manageSend/sendList',
                    },
                    children: [
                        {
                            path: 'sendList',
                            name: 'sendList',
                            component: () => import('@/views/employer/manageSend/sendList/index.vue'),
                            meta: {
                                title: '投递列表',
                                requiresAuth: true,
                                pathName: 'addJob',
                                titleName: 'manageSend/sendList',
                            }
                        },
                        {
                            path: 'jobAndResumeDetail/:sendId',
                            name: 'jobAndResumeDetail',
                            component: () => import('@/views/employer/manageSend/sendDetail/JobAndResumeDetail.vue'),
                            meta: {
                                title: '投递详情',
                                requiresAuth: true,
                                pathName: 'jobAndResumeDetail',
                                titleName: 'manageSend/sendList',
                            }
                        }
                    ]
                },
                {
                    path: 'manageInterview',
                    name: 'employerManageInterview',
                    component: () => import('@/views/employer/manageInterview/index.vue'),
                    meta: {
                        title: '面试管理',
                        requiresAuth: true,
                        pathName: 'manageInterview',
                        titleName: 'manageInterview/interviewList',
                    },
                    children: [
                        {
                            path: 'interviewList',
                            name: 'interviewList',
                            component: () => import('@/views/employer/manageInterview/interviewList/index.vue'),
                            meta: {
                                title: '面试列表',
                                requiresAuth: true,
                                pathName: 'interviewList',
                                titleName: 'manageInterview/interviewList',
                            }
                        },
                        {
                            path: 'interviewDetail/:interviewId',
                            name: 'interviewDetail',
                            component: () => import('@/views/employer/manageInterview/interviewDetail/interviewDetail.vue'),
                            meta: {
                                title: '面试详情',
                                requiresAuth: true,
                                pathName: 'interviewDetail',
                                titleName: 'manageInterview/interviewList',
                            }
                        }
                    ]
                },
                {
                    path: 'manageOffer',
                    name: 'employerManageOffer',
                    component: () => import('@/views/employer/manageOffer/index.vue'),
                    meta: {
                        title: 'Offer管理',
                        requiresAuth: true,
                        pathName: 'manageOffer',
                        titleName: 'manageOffer/offerList',
                    },
                    children: [
                        {
                            path: 'addOffer',
                            name: 'addOffer',
                            component: () => import('@/views/employer/manageOffer/addOffer/index.vue'),
                            meta: {
                                title: '添加Offer',
                                requiresAuth: true,
                                pathName: 'addOffer',
                                titleName: 'manageOffer/offerList',
                            }
                        },
                        {
                            path: 'offerList',
                            name: 'EmployerManageOfferOfferList',
                            component: () => import('@/views/employer/manageOffer/offerList/index.vue'),
                            meta: {
                                title: 'Offer列表',
                                requiresAuth: true,
                                pathName: 'offerList',
                                titleName: 'manageOffer/offerList',
                            }
                        },
                    ]
                },
                {
                    path: 'manageNew',
                    name: 'employerManageNew',
                    component: () => import('@/views/employer/manageNew/index.vue'),
                    meta: {
                        title: '新消息',
                        requiresAuth: true,
                        pathName: 'manageNew',
                        titleName: 'manageNew/newList',
                    },
                    children: [
                        {
                            path: 'newList',
                            name: 'EmployerManageNewNewList',
                            component: () => import('@/views/employer/manageNew/newList/index.vue'),
                            meta: {
                                title: '新消息列表',
                                requiresAuth: true,
                                pathName: 'newList',
                                titleName: 'manageNew/newList',
                            }
                        }
                    ]

                },
                {
                    path: 'employerReport',
                    name: 'employerReport',
                    component: () => import('@/views/employer/employerReport/index.vue'),
                    meta: {
                        title: '举报',
                        requiresAuth: true,
                        pathName: 'employerReport',
                        titleName: 'employerReport',
                    },
                },
                {
                    path: 'employerReportDetail/:reportId',
                    name: 'employerReportDetail',
                    component: () => import('@/views/employer/employerReport/employerReportDetail.vue'),
                    meta: {
                        title: '举报详情',
                        requiresAuth: true,
                        pathName: 'employerReportDetail',
                        titleName: 'employerReport',
                    }
                },
            ]
        }
    ],

})

router.beforeEach((to, from, next) => {
    const userStore = useInfoStore();
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
    if (requiresAuth && !userStore.isLogin) {
        // 记录页面访问来源，这里简单打印一下，实际可根据需求发送给后端等操作
        console.log(`用户从 ${from.path} 跳转到 ${to.path}`);
        // 如果需要认证的路由（通过meta.requiresAuth判断）且用户未登录（userStore.isLoggedIn为false）
        // 则跳转到登录页面
        next('/login');
    } else {
        next();
    }
});


export default router
